define([
    'style!./index.css'
], function() {
    'use strict';
    
    var currentPage = {
        render: function(){
            currentPage.resetData();
            var aStr = '';
            var data = currentPage.listData;
            data.forEach(function(item, index){
                aStr += '<tr class="table-body-tr">'
                    + '<td><div class="index">'+ (index+1) +'</div></td>'
                    + '<td><div class="product"><div data-json=\''+ JSON.stringify(item) +'\' id="'+ currentPage.boxId + '_' + (index) +'"></div></div></td>'
                    + '<td><div class="days"><input value="'+ (item.day || 0) +'" placeholder="请输入天数" /></div></td>'
                    + '<td><div class="percent">'+ (item.percent || 0) +'%</div></td>'
                    + '<td><div class="handle"><a class="handle-btn" data-type="delete" href="javascript:void(0)">删除</a></div></td>'
                    + '</tr>';
            })

            var str = '<div class="yby-table-container">'
                + '<table class="yby-table">'
                + '<colgroup>'
                + '<col width="10%">'
                + '<col width="30%">'
                + '<col width="30%">'
                + '<col width="20%">'
                + '<col width="10%">'
                + '</colgroup>'
                + '<thead class="yby-table-thead">'
                + '<tr><th>序号</th><th>复用产品</th><th>天数</th><th>百分比</th><th></th></tr>'
                + '</thead>'
                + '<tbody class="yby-table-tbody">'
                + aStr
                + '<tr class="table-body-tr handle"><td></td><td colspan="4"><button class="btn" data-type="add">新增</button></td></tr>'
                + '<tr class="table-body-tr count"><td></td><td>合计</td><td><div class="count"></div></td><td colspan="2"></td></tr>'
                + '</tbody>'
                + '</table>'
                + '</div>';
            $("#" + currentPage.boxId).html(str);
            currentPage.renderSelect();
        },
        selectObj: {},
        renderSelect: function(){
            $('#' + currentPage.boxId).find('.table-body-tr .product>div').map(function(index, item){
                var aId = $(item).attr('id');
                var json = $(item).data('json');
                if(currentPage.renderProduct){
                    currentPage.renderProduct(aId, json);
                }
            })
        },
        changeData: function(data){
            currentPage.listData = data;
            currentPage.render();
        },
        addItem: function(){
            currentPage.listData.push({
                day: 0,
                percent: 0
            });
            currentPage.render();
        },
        removeItem: function(_this){
            var tr = $(_this).parents('tr.table-body-tr').eq(0);
            var aIndex = tr.index();
            currentPage.listData.splice(aIndex, 1);
            currentPage.render();
        },
        clear: function(){
            currentPage.listData = [];
            currentPage.render();
        },
        listenFunc: {
            btnClick: function(e){
                var _this = e.target;
                var type = $(_this).data('type');
                if(type == 'add'){
                    currentPage.addItem();
                }
            },
            itemClick: function(e){
                var _this = e.target;
                var type = $(_this).data('type');
                if(type == 'delete'){
                    currentPage.removeItem(_this);
                }
            },
            inputBlur: function(e){
                var _this = e.target;
                var tr = $(_this).parents('tr').eq(0);
                var aIndex = tr.index();
                var value = $(_this).val();
                currentPage.listData[aIndex].day = value;
                currentPage.render();
            }
        },
        setListener: function(){
            var id = currentPage.boxId;

            // 对新增的监听
            $(document).off('click', '#'+ id +' .yby-table-container .table-body-tr.handle button', currentPage.listenFunc.btnClick);
            $(document).on('click', '#'+ id +' .yby-table-container .table-body-tr.handle button', currentPage.listenFunc.btnClick);

            // 删除事件监听
            $(document).off('click', '#'+ id +' .yby-table-container .table-body-tr .handle .handle-btn', currentPage.listenFunc.itemClick);
            $(document).on('click', '#'+ id +' .yby-table-container .table-body-tr .handle .handle-btn', currentPage.listenFunc.itemClick);

            // 输入框失去焦点事件监听
            $(document).off('blur', '#'+ id +' .yby-table-container .table-body-tr .days>input', currentPage.listenFunc.inputBlur);
            $(document).on('blur', '#'+ id +' .yby-table-container .table-body-tr .days>input', currentPage.listenFunc.inputBlur);
        },
        resetData: function(){
            currentPage.listData = currentPage.listData.length <= 0 ? [{}] : currentPage.listData;
            currentPage.totalDay = 0;
            currentPage.listData.forEach(function(item){
                var aDay = !item.day ? 0 : parseInt(item.day) == 'NaN' ? 0 : parseInt(item.day);
                currentPage.totalDay += aDay;
            })
            currentPage.listData = currentPage.listData.map(function(item){
                item.day = !item.day ? 0 : parseInt(item.day) == 'NaN' ? 0 : parseInt(item.day);
                item.percent = item.day == 0 ? 0 : (item.day * 100 / currentPage.totalDay).toFixed(0);
                return item;
            })
        },
        init: function(options){
            currentPage.boxId = options.id;
            currentPage.listData = !options.data || options.data.length <= 0 ? [] : JSON.parse(JSON.stringify(options.data));
            currentPage.renderProduct = options.renderProduct;
            currentPage.render();
            currentPage.setListener();
        }
    }

    return currentPage;
});